package com.toprs.base.controller

import com.toprs.base.vo.UpdatePasswordVO
import com.toprs.base.entity.User
import com.toprs.base.entity.UserBase
//import com.toprs.base.entity.UserRole
import com.toprs.base.query.UserQuery
import com.toprs.base.result.PageResult
import com.toprs.base.result.Result
import com.toprs.base.service.UserService
import com.toprs.base.utils.JwtUtil
import com.toprs.base.vo.UserVO
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.servlet.http.HttpServletRequest
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid


@RestController
@RequestMapping("/user")
@Tag(name = "用户模块")
class UserController(val userService: UserService) {

    @PostMapping("/add")
    @Operation(summary = "添加用户")
    fun addUser(@RequestBody user: User): Result<Unit> {
        userService.addUser(user)
        return Result.successMsg("添加用户成功")
    }

    @GetMapping("/delete/{id}")
    @Operation(summary = "删除用户")
    fun deleteUser(@PathVariable id: String): Result<Unit?> {
        userService.deleteUser(id)
        return Result.success(null)
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "获取用户信息")
    fun infoUser(@PathVariable id: String): Result<User?> {
        val user = userService.getUserInfo(id)
        return Result.success(user)
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    fun loginUser(@RequestBody userVO: UserVO, httpServletRequest: HttpServletRequest): Result<Map<String, Any>> {
        val user = userService.login(userVO, httpServletRequest)
        val result = mapOf("userInfo" to user, "token" to JwtUtil.createToken(user))
        return Result.success(result)
    }

    @GetMapping("/logout/{id}")
    @Operation(summary = "用户退出登录")
    fun logout(@PathVariable id: String, httpServletRequest: HttpServletRequest): Result<Unit?> {
        userService.logOut(id, httpServletRequest)
        return Result.success(null)
    }


    @PostMapping("/query")
    @Operation(summary = "分页查询用户")
    fun queryUser(@Valid @RequestBody query: UserQuery): Result<PageResult<UserBase>> {
        return Result.success(userService.queryUser(query))
    }


    @GetMapping("/all-list")
    @Operation(summary = "模糊查询获取用户")
    fun getAllList(realname: String = ""): Result<List<UserBase>> {
        return Result.success(userService.getAllList(realname))
    }

    @PostMapping("/edit")
    @Operation(summary = "编辑用户信息")
    fun editUser(@RequestBody user: User): Result<Unit> {
        userService.editUser(user)
        return Result.successMsg("编辑用户成功！")
    }


    @GetMapping("/reset/{id}")
    @Operation(summary = "初始化密码")
    fun resetUserPassword(@PathVariable id: String): Result<Unit> {
        val password = userService.resetUserPassword(id)
        return Result.successMsg("用户密码初始化为${password}！")
    }

    @PostMapping("/updateSelf")
    @Operation(summary = "用户自己编辑用户信息")
    fun updateSelf(@RequestBody user: User): Result<Unit> {
        userService.updateSelf(user)
        return Result.successMsg("更新信息成功！")
    }

    @GetMapping("/getUserInfoSelf")
    @Operation(summary = "用户获取自己用户信息")
    fun getUserInfoSelf(): Result<User> {
        return Result.success(userService.getUserInfoSelf())
    }

    @GetMapping("/checkPass/{old}")
    @Operation(summary = "检查旧密码")
    fun checkPass(@PathVariable old: String): Result<Unit> {
        userService.checkPassOld(old)
        return Result.successMsg("")
    }


    @PostMapping("/updatePassword")
    @Operation(summary = "更新密码")
    fun updatePassword(@RequestBody updatePassEntity: UpdatePasswordVO): Result<Unit> {
        userService.updatePassword(updatePassEntity)
        return Result.successMsg("密码修改成功")
    }


}